package com.zmm.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmm.weblog.common.domain.dos.ArticleDo;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
@Mapper
public interface ArticleMapper extends BaseMapper<ArticleDo> {

    default Page<ArticleDo> selectArticlePage(Long current, Long size, String title, LocalDate startDate, LocalDate endDate) {
        Page<ArticleDo> page = new Page<>(current,size);
        LambdaQueryWrapper<ArticleDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(title), ArticleDo::getTitle,title)
                .ge(Objects.nonNull(startDate), ArticleDo::getCreateTime,startDate)
                .le(Objects.nonNull(endDate), ArticleDo::getCreateTime,endDate)
                .orderByDesc(ArticleDo::getCreateTime);//按创建时间倒叙
        return selectPage(page,wrapper);
    }

    default Page<ArticleDo> selectPageList(Long current, Long size, String title, LocalDate startDate, LocalDate endDate){
        //分页对象(查询第几页，每页多少数据)
        Page<ArticleDo> page = new Page<>(current, size);
        //构建查询条件
        LambdaQueryWrapper<ArticleDo> wrapper = new LambdaQueryWrapper<ArticleDo>()
                .like(StringUtils.isNotBlank(title), ArticleDo::getTitle, title)
                .ge(Objects.nonNull(startDate), ArticleDo::getCreateTime, startDate)
                .le(Objects.nonNull(endDate), ArticleDo::getCreateTime, endDate)
                .orderByDesc(ArticleDo::getCreateTime);//按创建时间倒叙

        return selectPage(page, wrapper);
    }

    /**
     * 根据文章id 批量分页查询
     * @param current
     * @param size
     * @param articleIds
     * @return
     */
    default Page<ArticleDo> selectPageListByArticleIds(Long current, Long size, List<Long> articleIds) {
        //分页对象(查询第几页，每页多少数据)
        Page<ArticleDo> page = new Page<>(current, size);
        //构建查询条件
        LambdaQueryWrapper<ArticleDo> wrapper = new LambdaQueryWrapper<ArticleDo>()
                .in(Objects.nonNull(articleIds), ArticleDo::getId, articleIds)
                .orderByDesc(ArticleDo::getCreateTime);//按创建时间倒叙

        return selectPage(page, wrapper);
    }

    /**
     * 查询上一篇文章
     * @param articleId
     * @return
     */
    default ArticleDo selectPreArticle(Long articleId){
        return selectOne(new LambdaQueryWrapper<ArticleDo>()
                .orderByAsc(ArticleDo::getId)
                .gt(ArticleDo::getId,articleId)
                .last("limit 1"));//第一条记录为上一篇文章
    }

    /**
     * 查询下一篇文章
     * @param articleId
     * @return
     */
    default ArticleDo selectNextArticle(Long articleId){
        return selectOne(new LambdaQueryWrapper<ArticleDo>()
                .orderByAsc(ArticleDo::getId)
                .lt(ArticleDo::getId,articleId)
                .last("limit 1"));//第一条记录为下一篇文章
    }

    default int increaseReadNum(Long articleId){
        return update(null,new LambdaUpdateWrapper<ArticleDo>()
                .setSql("read_num = read_num + 1")
                .eq(ArticleDo::getId,articleId));

    }

    /**
     * 查询所有记录的阅读数
     * @return
     */
    default List<ArticleDo> selectAllReadNum(){
        return selectList(new LambdaQueryWrapper<ArticleDo>().orderByDesc(ArticleDo::getReadNum));
    }
}